package com.demo0211;

import java.util.Arrays;

/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2023-02-11
 * Time: 13:37
 */
public class SeqList {
    // 顺序表名
    public int[] elem;
    // 有效元素
    public int usedSize;
    // 顺序表容量
    public static int capacity = 10;

    // 使用构造方法创建顺序表
    public SeqList() {
        this.elem = new int[capacity];
    }

    // 判断顺序表是否满
    public boolean isFull() {
//        if (this.usedSize == capacity) {
//            return true;
//        }
//        return false;
        return this.usedSize == capacity;
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if (pos < 0 || pos > capacity) {
            System.out.println("pos 位置不合理");
            return;
        }
        if (isFull()) {
            this.elem = Arrays.copyOf(this.elem, capacity*2);
            capacity *= 2;
        }
        for (int i = this.usedSize-1; i >= pos; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    // 打印顺序表
    public void display() {
        if (this.usedSize == 0) {
            System.out.println("顺序表为空");
            return;
        }
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    // 判断顺序表是否为空
    public boolean isEmpty() {
        return this.usedSize == 0;
    }

    // 判断是否包含某个元素
    public boolean contains(int toFind) {
        if (isEmpty()) {
            return false;
        }
        for (int data : this.elem) {
            if (toFind == data) {
                return true;
            }
        }
        return false;
    }

    // 查找某个元素对应的位置
    public int search(int toFind) {
        if (isEmpty()) {
            System.out.println("顺序表为空！");
            return -1;
        }
        int i = -1;
        for (int data : this.elem) {
            if (data == toFind) {
                return i+1;
            }
            i++;
        }
        return i;
    }

    // 获取 pos 位置的元素
    public int getPos(int pos) {
        if (isEmpty()) {
            System.out.println("表空");
            return -1;
        }
        if (pos < 0 || pos > this.usedSize) {
            System.out.println("pos 越界");
            return -1;
        }
        return this.elem[pos];
    }

    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }

    // 将 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
        if (isEmpty()) {
            System.out.println("表为空");
            return;
        }
        if (pos > size() || pos < 0) {
            System.out.println("pos 越界");
            return;
        }
        this.elem[pos] = value;
    }

    // 删除第一次出现的关键字 key
    public void remove(int key) {
        if (isEmpty()) {
            System.out.println("空表");
            return;
        }
        for (int i = 0; i < size(); i++) {
            if (this.elem[i] == key) {
                while (i < size()) {
                    this.elem[i] = this.elem[i+1];
                    i++;
                }
                this.usedSize--;
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("没找到 key");
        return;
    }

    // 清空表
    public void clear() {
        for (int i = 0; i < size(); i++) {
            this.elem[i] = 0;
        }
        this.usedSize = 0;
        System.out.println("成功清空顺序表");
        return;
    }
}
